<?php

namespace app\pc\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;



//资金流水日志
class Money extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("UsersMoney");
        parent::__construct();
        $rows = Cache::get($this->Cstatic->get("sid"));
        $this->sid_user_id = $rows['id'];
        $this->sid_mobile = $rows['mobile'];

    }


    public function read(){


        $user_id = $this->sid_user_id;
        $UsersMoney= new \app\np\model\UsersMoney; //用户资产
        $OrderDetails= new \app\np\model\OrderDetails;//订单详情
        $gpmoney = 0;
        //获取正在持仓的股票
        $rows_ = $OrderDetails->alias("a")
            ->field("a.Initial_price,a.equity_nums,shares.code")
            ->join("shares","shares.id=a.stock_id","left")
            ->where("a.user_id",$user_id)->where("a.status",2)->select();

        if(!empty($rows_)){
            foreach($rows_  as $k=>$v){
                $codemoney = gethq($v['code']);//最新价
             //   echo $codemoney."==".$v['equity_nums']."<br>";
                $gpmoney += bcmul($codemoney,$v['equity_nums'],2);
            }
        }


        $users =   $UsersMoney->field("created_at,advisable,stock_value,freeze,money,advisable,balance")->where("user_id",$user_id)->find();


        if($users) {
            $users['gpmoney'] = $gpmoney;//股票总资产
            $users['all'] = $users['balance'] + $gpmoney;//股票总资产
            return sucessJson($this->ecLang("scuessInsert"),$users);
        }

        $t_data = [
            "created_at"=>date("Y-m-d") ,
            "advisable"=>"0.00",
            "gpmoney"=>"0.00",
            "freeze"=>"0.00","money"=>"0.00","advisable"=>"0.00","balance"=>"0.00","all"=>"0.00"
        ];


        return   sucessJson($this->ecLang("failInsert"),$t_data);




    }



    //用户前台充值
    public function recharge(){
        //身份验证通过 银行卡绑定

        $this->deposit = abs($this->deposit);
        $UsersRecharge = model("UsersRecharge");
        $UsersMoney = model("UsersMoney");
        $cMcount = $UsersMoney->where('user_id',$this->sid_user_id)->count();
        if($cMcount==0){
            //新增
            $ttData = ['user_id'=>$this->sid_user_id];
            $kds = $UsersMoney->save($ttData);
            if(!$kds){
                return sucessJson("系统异常..无法充值");
            }
        }


        $data = [
            "deposit"=>$this->deposit,
            "user_id"=>$this->sid_user_id,
           // "bank_id"=>$this->bank_id,
            "type"=>1,
            "pay_no"=>ordersn()
        ];

        if(isset($this->auto)&&$this->auto==1){
            //


            $UsersInvest = model("UsersInvest");
            $where['user_id'] = $this->sid_user_id;
            //时间的处理

            $result = $UsersInvest->where($where)->find();

            if(empty($result)){
                failJson("请先完成实名认证之后在充值");
            }


            //自動充值

            if(!empty($result)){
                $resultPay = byThrityPayPC($this->deposit,$data['pay_no'],$result['user_name'],$result['id_number'],$this->sid_mobile,isset($this->bank_id)?$this->bank_id:0);

                if(!$resultPay['status']){
                    if(isset($resultPay['msg'])){
                        return failJson($resultPay['msg']);
                    }
                   return failJson("暂时不能在线支付,抱歉");
                }
                $data['thirdparty_payno'] = $data['pay_no'];
                $urls = $resultPay['data']['url'];

            }else{
                return   failJson($this->ecLang("basicCountfail"));
            }

        }

       $res =  $UsersRecharge->save($data);
        if(!$res){
            return   failJson($this->ecLang("failInsert"));
        }
        if(isset($urls)){
            $d = ["url"=>$urls];
        }else{
            $d = [];
        }
        return sucessJson($this->ecLang("scuessInsert"),$d);
        //"status" deposit user_id type
        //充值
    }

    //用户提现
    public function withdraw(){
        //身份验证通过 银行卡绑定
        $UsersBank = model("UsersBank");
        $UsersRecharge = model("UsersRecharge");
        $this->deposit = abs($this->deposit);

    //银行卡的个数
        $UsersInvest  = model("UsersInvest");
        $resx = $UsersInvest->where('user_id',$this->sid_user_id)->find();
        if(empty($resx)){
            return   failJson("請完成认证");
        }
        if($resx['status']!=1){
            return   failJson("请先完成认证");
        }

       $bankcount  = $UsersBank->where("user_id",$this->sid_user_id)->count();
           if(!$bankcount){
               return   failJson("你还未绑定银行卡");
           }
        //

        $money =  $this->model->where("user_id",$this->sid_user_id)->find();
        if(empty($money)){
            return   failJson($this->ecLang("password"));
        }
        if($money['advisable']<$this->deposit){
            return   failJson($this->ecLang("advisableisit"));
        }

        if(md5($this->transact_password.$money['ealt'])!=$money['transact_password']){
            return   failJson($this->ecLang("passwordfail"));
        }

        $data = [
            "deposit"=>$this->deposit,
            "user_id"=>$this->sid_user_id,
            "bank_id"=>$this->bank_id,
            "type"=>2,
            "pay_no"=>ordersn()
        ];
        $res =  $UsersRecharge->save($data);
        if(!$res){
            return   failJson($this->ecLang("failInsert"));
        }
        return sucessJson($this->ecLang("scuessInsert"));
        //"status" deposit user_id type
        //充值


    }


    /**
     * @return \think\response\Json 发送验证码
     */
    public function sendmessage(){
          $code=mt_rand(1000,9999);
            $phone = $this->sid_mobile;
           sedMess($phone,$code);
        Cache::set($this->Cstatic->get("sid")."mcode",$code,3600);
        return sucessJson($this->ecLang("scuessInsert"));
    }


    /**
     * @return \think\Response
     */
    public function rcode(){
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    30,
            // 验证码位数
            'length'      =>    4,

            // 关闭验证码杂点
            'useNoise'    =>    false,
        ];
        $sid =  Request::instance()->token( '__token__',  'md5');
        $salt = substr($sid,0,4);

        $codes = explode(" ",$salt);

        Cache::set($this->Cstatic->get("sid")."code",$salt,3600);
        return captcha("1",$config,$codes);
    }


    public function setpassword(){
        //设置密码

        $code = Cache::get($this->Cstatic->get("sid")."code");
        $mcode = Cache::get($this->Cstatic->get("sid")."mcode");
        if(trim($code) !=$this->code){
            return   failJson($this->ecLang("codefail"));
        }
        if(trim($mcode) !=$this->mcode){
            return   failJson($this->ecLang("mcodefail"));
        }

       $moneys =  $this->model->where("user_id",$this->sid_user_id)->count();
        $sid =  Request::instance()->token( '__token__',  'md5');
        $salt = substr($sid,0,4);
        $data = [
            "transact_password"=> md5($this->transact_password.$salt),
            "user_id"=> $this->sid_user_id,
            "ealt"=> $salt,
        ];
        if(!empty($moneys)){ //设置
            $res =  $this->model->save($data,['user_id'=>$this->sid_user_id]);
        }else{
            $res =  $this->model->save($data);
        }

        if(!$res){
            return   failJson($this->ecLang("failInsert"));
        }
        return sucessJson($this->ecLang("scuessInsert"));
}

    //修改密码

    public function changepassword(){


        $sid =  Request::instance()->token( '__token__',  'md5');
        $salt = substr($sid,0,4);
        $Users = model("Users");
        $row = $Users->where("id",$this->sid_user_id)->find();




        if(md5($this->oldtransact_password.$row['slat'])==$row['password']){
            //修正密码成功
            $res = $Users->where("id",$this->sid_user_id)->update(["password"=>md5($this->transact_password.$salt),"slat"=>$salt,"secure"=>$this->transact_password]);

            if(!$res){
                return   failJson($this->ecLang("failInsert"));
            }
            return sucessJson($this->ecLang("scuessInsert"));
        }else{
            return   failJson($this->ecLang("failInsert"));
        }



    }

}
